<?php
namespace app\admin\logic;
use app\common\model\ShopConfig;
use think\Db;

class RechargeLogic{
    public static function templatelists(){
        $list = Db::name('recharge_template')->where(['delete_time'=>0])->order('id desc')->select();
        foreach ($list as &$item){
            $item['money'] && $item['money'] = '￥'.$item['money'];
            $item['give_money'] && $item['give_money'] = '￥'.$item['give_money'];
        }
        return $list;
    }

    public static function getRechargeConfig(){
       $config =  [
           // 'open_racharge'  => ConfigServer::get('recharge','open_racharge',0),
           // 'give_integral'  => ConfigServer::get('recharge', 'give_integral', ''),
           // 'give_growth'    => ConfigServer::get('recharge', 'give_growth', ''),
           'min_money'      => ShopConfig::get('recharge', 'min_money', ''),
       ];
       return [$config];
    }
    public static function setRecharge($post){
        // ConfigServer::set('recharge','open_racharge',$post['open_racharge']);
        // ConfigServer::set('recharge','give_integral',$post['give_integral']);
        // ConfigServer::set('recharge','give_growth',$post['give_growth']);
        ShopConfig::set('recharge','min_money', round($post['min_money'], 2));
    }

    public static function add($post){
        $new = time();
        $add_data = [
            'money'         => $post['money'],
            'give_money'    => $post['give_money'],
            // 'sort'          => $post['sort'],
            // 'is_recommend'  => $post['is_recommend'],
            'create_time'   => $new,
            'update_time'   => $new,
        ];
        return Db::name('recharge_template')->insert($add_data);
    }

    public static function edit($post){
        $new = time();
        $update_data = [
            'money'         => $post['money'],
            'give_money'    => $post['give_money'],
            // 'sort'          => $post['sort'],
            // 'is_recommend'  => $post['is_recommend'],
            'update_time'   => $new,
        ];
        return Db::name('recharge_template')->where(['id'=>$post['id']])->update($update_data);
    }

    public static function getRechargeTemplate($id){
        return Db::name('recharge_template')->where(['id'=>$id])->find();
    }


    public static function del($id){
        return Db::name('recharge_template')->where(['id'=>$id])->update(['update_time'=>time(),'delete_time'=>time()]);
    }


    /**
     * note 修改制定表的某个字段
     * author cjh 2020/10/14 14:51
     * @param $table 表名
     * @param $pk_name  id
     * @param $pk_value id的值
     * @param $field 需要修改的字段
     * @param $field_value  需要修改的值
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function changeTableValue($table,$pk_name,$pk_value,$field,$field_value){
        //允许修改的字段
        $allow_field = [
            'is_show','sort','status','is_new','is_best','is_like','is_recommend'
        ];
        if(!in_array($field,$allow_field)){
            return false;
        }
        if(is_array($pk_value)){
            $where[] = [$pk_name,'in',$pk_value];
        }else{
            $where[] = [$pk_name,'=',$pk_value];
        }

        $data= [
            $field          => $field_value,
            'update_time'   => time(),
        ];

        if ($field == 'status' and $field_value == 0) {
            $activity_goods = GoodsLogic::activityGoods()['activity_goods'];
            if(in_array($pk_value,$activity_goods)){
                return '该商品正在参与活动，请先关闭后才允许下架';
            }

        }

        $result = Db::name($table)->where($where)->update($data);
        return $result ? true : '修改失败';
    }
}